package com.huobaibai.service.security;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.huobaibai.dao.UserDao;
import com.huobaibai.domain.entry.UserEO;

/**
 * 通用的【用户授权】配置类
 * 自定义实现 UserDetailsService
 * @author Jian.Yang
 *
 */

public class CustomUserService implements UserDetailsService {
	@Autowired
	private UserDao userDao;
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		System.out.println("use CustomUserService");
//		UserEO user=userDao.findByNameOne(username);
		
		UserEO user=userDao.findByUserId(username);
		//注册角色
//		List<GrantedAuthority>  authorities=new ArrayList<GrantedAuthority>();
//		authorities.add(new SimpleGrantedAuthority("ROLE_NAME"));
//		return new User(user.getName(),user.getPassword(),authorities);
		if (user==null)
		{
			throw new UsernameNotFoundException("用户名不存在");
		}
		return user;
		
	}

}
